package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcAnalysis;
import com.csun.cmny.provider.model.dto.OldManBirthdayDto;
import com.csun.cmny.provider.pojo.BirthdayRemind;
import com.csun.cmny.provider.pojo.Building;
import com.csun.cmny.provider.pojo.SexCountVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.DateUtils;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/*
 * 统计数据分析
 * */
@RestController
@RequestMapping("/analysis")
public class AnalysisController extends BaseController {

    @Autowired
    private OldmanCheckInService oldmanCheckInService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private BedService bedService;
    @Autowired
    private OmcAnalysisService omcAnalysisService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 入院老人男女人数统计
     */
    @RequestMapping(value = "/getSexAnalysis",method = RequestMethod.GET)
    public Wrapper sexAnalysis() {
        List<SexCountVo> voList = oldmanCheckInService.getSexAnalysis(getLoginAuthDto().getGroupId());
//        if (voList.size() <= 0) {
//            return WrapMapper.error("未查询到记录");
//        } else {
//            return  WrapMapper.ok(voList);
//        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), voList);
    }
    /**
     * 统计入院老人、居家老人比列
     */
    @RequestMapping(value = "/gerOldTypeAnalysis",method = RequestMethod.GET)
    public Wrapper gerOldTypeAnalysis() {
        List<SexCountVo> voList = oldmanCheckInService.gerOldTypeAnalysis(getLoginAuthDto().getGroupId());
//        if (voList.size() <= 0) {
//            return WrapMapper.error("未查询到记录");
//        } else {
//            return  WrapMapper.ok(voList);
//        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), voList);
    }


    /**
     * 老人人数增长分析
     */
    @RequestMapping(value = "/oldTypeAnalysis",method = RequestMethod.GET)
    public Wrapper oldTypeAnalysis(Integer year) {
        if (year==null){
            return WrapMapper.error("年份不能为空");
        }
        int[] list = {1,2,3,4,5,6,7,8,9,10,11,12};
        Long groupId = getLoginAuthDto().getGroupId();
        Map map =null;
        List<SexCountVo> voList = new ArrayList<>();
        for (int month:list) {
            map =new HashMap();
            map.put("month",month);
            map.put("year",year);
            map.put("homeId",groupId);
            SexCountVo vo=    oldmanCheckInService.getOldmanNalysis(map);
            voList.add(vo);
        }
//        if (voList.size() <= 0) {
//            return WrapMapper.error("未查询到记录");
//        } else {
//            return WrapMapper.ok(voList);
//        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), voList);
    }

    /**
     * 院内老人和居家老人分析
     */
    @RequestMapping(value = "/oldTypeByMonth",method = RequestMethod.GET)
    public Wrapper oldTypeByMonth(Integer year) {
        if (year==null){
            return WrapMapper.error("年份不能为空");
        }
        int[] list = {1,2,3,4,5,6,7,8,9,10,11,12};
        Long groupId = getLoginAuthDto().getGroupId();
        List<SexCountVo> voList = new ArrayList<>();
        Calendar now = Calendar.getInstance();
        int nowMonth = now.get(Calendar.MONTH)+1;
        int nowYear = now.get(Calendar.YEAR);
        Map map = new HashMap();
        map.put("homeId",groupId);
        map.put("status",3);
        for (int month:list) {
            String saveTime = year.toString()+'-'+month;
            map.put("saveTime",saveTime);
            OmcAnalysis oa = omcAnalysisService.selectByMonth(map);
            map.put("year",year);
            map.put("month",month);
            SexCountVo vo = oldmanCheckInService.getOldmanNalysis(map);
            if(month != nowMonth){
                vo.setRuyuan(0);
                vo.setJujia(0);
                if(oa!=null){
                    if(oa.getHomeCount()!=null){
                    vo.setJujia(oa.getHomeCount());}
                    if(oa.getOnCount()!=null){
                    vo.setRuyuan(oa.getOnCount());}
                }

            }else if(year==nowYear){
                map.put("year",null);
                map.put("month",null);
                vo = oldmanCheckInService.getOldmanNalysis(map);
            }
            voList.add(vo);
        }
//        if (voList.size() <= 0) {
//            return WrapMapper.error("未查询到记录");
//        } else {
//            return WrapMapper.ok(voList);
//        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), voList);
    }

    /**
     * 老人生日提醒
     */
    @RequestMapping(value = "/oldBirthdayRemind",method = RequestMethod.GET)
    public Wrapper oldBirthdayRemind(String date){
        if (StringUtils.isEmpty(date)){
            return WrapMapper.error("日期不能为空");
        }
        OldManBirthdayDto oldManBirthdayDto = new OldManBirthdayDto();
        oldManBirthdayDto.setGroupId(getLoginAuthDto().getGroupId());

        List<String> threeDate=new ArrayList<>();
        String[] dates = DateUtils.forTimeByDateThreeDays(date);
        for (int i = 0; i < dates.length; i++) {
            threeDate.add(dates[i]);
        }
        oldManBirthdayDto.setDates(threeDate);
        List<BirthdayRemind> oldmanList = oldmanCheckInService.getNewOldBirthdayRemind(oldManBirthdayDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), oldmanList);
    }

    /**
     * 中控台楼栋楼层信息
     */
    @RequestMapping(value = "/showBuildingAndElderInfo",method = RequestMethod.GET)
    public Wrapper showBuildingAndElderInfo(){
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Long homeId = loginAuthDto.getGroupId();
        Building building = new Building();
        building.setHomeId(homeId.intValue());
        List<Building> buildingList =  buildingService.showAlls(building);

//        return WrapMapper.ok(buildingList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), buildingList);
    }
}
